package com.ybt.communicate.ws.factory;

import com.ybt.communicate.core.IProtocol;
import com.ybt.communicate.core.configration.IProtocolConstants;
import com.ybt.communicate.core.exception.ProtocolException;
import com.ybt.communicate.core.factory.IProtocolFactory;
import com.ybt.communicate.ws.client.WSClientRPC;
import com.ybt.communicate.ws.configration.WSConstants;
import com.ybt.communicate.ws.configration.WSProtocolConfig;
import com.ybt.communicate.ws.configration.WSRuntimeConfig;

public class WSProtocolFactory implements IProtocolFactory<WSProtocolConfig> {

    /*
     * (non-Javadoc)
     * 
     * @see com.ybt.communicate.core.factory.IProtocolFactory#createProtocol(com.ybt.communicate.core.configration.IProtocolConfig)
     */
    public IProtocol createProtocol(WSProtocolConfig protocolConfig)
            throws ProtocolException {
        // TODO Auto-generated method stub
        WSRuntimeConfig rconfig = new WSRuntimeConfig(protocolConfig);
        IProtocol protocol = null;
        if (protocolConfig.isServerSide()) {
            // FIXME 目前 ws server 端使用axis 实现
            throw new ProtocolException("unsupport protocol type ["
                    + protocolConfig.getProtocolType() + "] for ["
                    + protocolConfig.getProtocolId() + "]");
        } else {
            // 作为客户端发送数据使用
            if (IProtocolConstants.MODE_DUPLEX.equals(protocolConfig
                    .getProtocolMode())) {
                // ws 双工同步协议
                rconfig.setDuplex(true);

                //wsdl 地址
                String target = protocolConfig.getCommonConfig().getTarget();
                rconfig.setTarget(target);

                //ws 模式，目前只支持rpc
                String mode = protocolConfig.getCommonConfig().getMode();
                rconfig.setMode(mode);

                //ws 方法
                String method = protocolConfig.getReqConfig().getMethod();
                rconfig.setMethod(method);

                //ws 名称空间
                String namespace = protocolConfig.getReqConfig()
                        .getTargetNamespace();
                rconfig.setTargetNamespace(namespace==null?"" : namespace);

                //调用rpc方法参数
                for (String p : protocolConfig.getReqConfig().getParameters()) {

                    if (p.startsWith(WSConstants.PARAM_TYPE_DYNAMIC)) {
                        //获取方法传递的参数的值
                        String value = p
                                .substring(WSConstants.PARAM_TYPE_DYNAMIC
                                        .length());
                        rconfig.addParameter(
                                WSConstants.PARAM_TYPE_DYNAMIC, value);
                    } else if (p.startsWith(WSConstants.PARAM_TYPE_PROPERTY)) {
                        //获取property中的值
                        String value = p
                                .substring(WSConstants.PARAM_TYPE_PROPERTY
                                        .length());
                        rconfig.addParameter(WSConstants.PARAM_TYPE_PROPERTY,
                                value);
                    } else {
                        //字面值
                        rconfig.addParameter(WSConstants.PARAM_TYPE_LITERAL, p);
                    }
                }
            } else {
                // ws单工异步协议，只用于发送数据
                // FIXME 目前 ws client 不支持单工
                throw new ProtocolException("unsupport protocol mode ["
                        + protocolConfig.getProtocolMode() + "] for ["
                        + protocolConfig.getProtocolId() + "]");
            }
            protocol = new WSClientRPC(rconfig);
        }
        return protocol;
    }

}
